#!/bin/bash

set -o errexit
set -o pipefail
export LC_ALL=C

NODES_NUM=$1
SYNC_DIR=$2
GENES_DIR=$3
RESULTS_DIR=$4

BLAST_DB_PATH="/home/mixagol/work/data/blast_db/GENOMES_DB"
RUN_BLAST_SCRIPT="/home/mixagol/work/gene-class/2_raw_matrix/run_blast.sh"

NODE_ID=`/home/mixagol/work/gene-class/tools/sync_workers.py $NODES_NUM $SYNC_DIR`

ls $GENES_DIR | awk '{if ($1 % '$NODES_NUM' == '$NODE_ID') print}' \
  | xargs -n1 -P1 -I {} bash -c "cat $GENES_DIR/{} | $RUN_BLAST_SCRIPT $BLAST_DB_PATH | gzip > $RESULTS_DIR/{}.gz"

